package org.linuxsogood.boot.nio;

import lombok.Cleanup;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * @author honway.liu
 * @date 2019/11/20 11:31 下午
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
public class FileChannelDemo {

    public static void main(String[] args) throws IOException {
        writeFileChannel();
    }

    private static void newCopy() throws IOException {
        @Cleanup FileInputStream fileInputStream = new FileInputStream("channel.txt");
        @Cleanup FileOutputStream fileOutputStream = new FileOutputStream("channel-copy-channel.txt");

        FileChannel inputStreamChannel = fileInputStream.getChannel();
        FileChannel outputStreamChannel = fileOutputStream.getChannel();

        outputStreamChannel.transferFrom(inputStreamChannel, 0, inputStreamChannel.size());
    }

    private static void copy() throws IOException {
        @Cleanup FileInputStream fileInputStream = new FileInputStream("channel.txt");
        @Cleanup FileOutputStream fileOutputStream = new FileOutputStream("channel-copy.txt");

        FileChannel inputStreamChannel = fileInputStream.getChannel();
        FileChannel outputStreamChannel = fileOutputStream.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(512);

        while (true) {
            buffer.clear();
            int read = inputStreamChannel.read(buffer);
            System.out.println("read = " + read);
            if (read == -1) {
                break;
            }
            buffer.flip();
            outputStreamChannel.write(buffer);
        }
    }

    private static void readFileViaChannel() throws IOException {
        File file = new File("channel.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel fileChannel = fileInputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        fileChannel.read(byteBuffer);

        System.out.println(new String(byteBuffer.array()));

        fileChannel.close();
        fileInputStream.close();
    }

    /**
     * 通过channel写入文件
     * @throws IOException
     */
    private static void writeFileChannel() throws IOException {
        String str = "hello, file channel.".concat(System.lineSeparator());
        // 创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream("channel.txt", true);

        // 通过输出流, 获取一个fileChannel
        FileChannel fileChannel = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(str);

        // 因为buffer是双向的
        // 反转buffer, 因为前面已经往buffer里写数据了
        // 这个时候buffer的position已经变了
        // 需要将buffer的position置为0, 才能读取到buffer中的数据
        //byteBuffer.flip();

        // 将byteBuffer写入到fileChannel中
        fileChannel.write(byteBuffer);

        fileChannel.close();
        fileOutputStream.close();
    }
}
